package com.code.zg.rxapplication.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.code.zg.rxapplication.R;
import com.code.zg.rxapplication.bean.User;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by zhanggang on 2017/9/13.
 */

public class APP2Adapter extends RecyclerView.Adapter<APP2Adapter.ViewHolder>{
    private List<User> mUsers = new ArrayList<>();
    private static ViewHolder.OpenProfileListener mProfileListener;

    public APP2Adapter(List<User> mUsers) {
        this.mUsers = mUsers;
    }
    public void updateUsers(List<User> users) {
        mUsers.clear();
        mUsers.addAll(users);
        notifyDataSetChanged();
    }
    public void setOpenProfileListener(ViewHolder.OpenProfileListener listener) {
        mProfileListener = listener;
    }
    @Override
    public APP2Adapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.so_list_item, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (position < mUsers.size()) {
            User user = mUsers.get(position);
            holder.setUser(user);
        }
    }


    @Override
    public int getItemCount() {
        return mUsers == null ? 0 : mUsers.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        private final View mView;

        @BindView(R.id.name)
        TextView name;

        @BindView(R.id.city)
        TextView city;

        @BindView(R.id.reputation)
        TextView reputation;

        @BindView(R.id.user_image)
        ImageView user_image;

        @BindView(R.id.city_image)
        ImageView city_image;


        public ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            mView = view;
        }
        public void setUser(final User user) {
            name.setText(user.display_name);
            city.setText(user.location);
            reputation.setText(String.valueOf(user.reputation));

            ImageLoader.getInstance().displayImage(user.profile_image, user_image);

//            displayWeatherInfos(user);

            mView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mProfileListener == null)return;
                        String url = user.website_url;
                    if (url != null && !url.equals("") && !url.contains("search")) {
                        mProfileListener.open(url);
                    } else {
                        mProfileListener.open(user.link);
                    }
                }
            });
//            v -> {
////                checkNotNull(mProfileListener, "Must implement OpenProfileListener");
//                if (mProfileListener == null)
//                    String url = user.getWebsiteUrl();
//                if (url != null && !url.equals("") && !url.contains("search")) {
//                    mProfileListener.open(url);
//                } else {
//                    mProfileListener.open(user.getLink());
//                }
//            });
        }
        public interface OpenProfileListener {

            public void open(String url);
        }

    }
}
